import { createApp } from 'vue'
import App from './App.vue'

// 引入总路由
import Router from './routers/index';

// 引入VantUI
import Vant from 'vant';

// 引入组件样式
import 'vant/lib/index.css';

// 引入Api
import Api from '@/api/index';

// 引入工具
import utils from '@/utils/utils';

// 引入cookie
import VueCookies from 'vue3-cookies';

const app = createApp(App)
.use(Router)
.use(Vant)
.use(Api)
.use(utils)
.use(VueCookies)
.mount('#app')

app.$router.beforeEach(async (to,from,next) => {
    let auth = to.meta.auth === true ? true : false;

    // 跳转组件需要登录
    if(auth === true)
    {
        // 获取cookie的登录信息
        let business = app.$cookies.get('business') ? app.$cookies.get('business') : {};

        if(!business || JSON.stringify(business) === '{}')
        {
            app.$showNotify({
                type:'warning',
                message:'请先登录',
                duration:1500
            });

            // 重定向登录页
            next('/business/base/login');
            return;
        }

        // 获取id和手机号
        let id = business.id ?? 0;
        let mobile = business.mobile ?? '';

        // 封装数据
        let data = {
            id,
            mobile
        }

        let result = await app.$api.check(data);

        if(result.code === 0)
        {
            app.$cookies.remove('business');

            app.$showNotify({
                type:'warning',
                message:result.msg,
                duration:1500
            });

            // 重定向登录页
            next('/business/base/login');
            return;
        }

        app.$cookies.set('business',result.data);
        next();
    }else{
        // 放行
        next();
    }
});